package dat255.calendarclock.model.event.interval;

import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Gabriel Andersson & Marcus Hultman
 */
public class WeeklyInterval extends Interval{

	private static final long serialVersionUID = -8246537397086236516L;
	//Field
	private List<Integer> activeDays;
	private boolean[] values;
	
	/**
	 * @author Gabriel Andersson
	 * @param activeDays the array of size 7 holding information about occurrence.
	 * @throws IllegalArgumentException if length of param is wrong.
	 */
	public WeeklyInterval(boolean[] activeDays){
		if(activeDays.length!=7)
			throw new IllegalArgumentException("Length must be 7, not "+activeDays.length+"!");
		this.values=activeDays;
		this.activeDays = new LinkedList<Integer>();
		for (int i = 0; i < activeDays.length; i++)
			if (activeDays[i])
				this.activeDays.add(((i + 1) % 7) + 1);
	}
	/**
	 * @author Marcus Hultman
	 */
	@Override
	public GregorianCalendar nextDateOfEvent(GregorianCalendar currentDate) {
		while(true){
			currentDate.add(GregorianCalendar.DATE, 1);
			for (Integer activeDay : activeDays){
				if (currentDate.get(GregorianCalendar.DAY_OF_WEEK) == activeDay){
					return currentDate;
				}
			}
		}
	}
	

	public boolean[] getValues(){
		return this.values;
	}
	
	public List<Integer> getActiveDays() {
		return activeDays;
	}

	public boolean equals(Interval interval) {
		if(interval instanceof WeeklyInterval) {
			return this.values.equals(((WeeklyInterval) interval).getValues());
		}
		else {
			return false;
		}
			
	}

}
